package com.hongyi.modelcontest.modular.wechat.controller;


import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.hongyi.modelcontest.core.util.Base64Util;
import com.hongyi.modelcontest.core.util.SpringbootUtils;
import com.hongyi.modelcontest.modular.system.entity.User;
import com.hongyi.modelcontest.modular.system.service.UserService;
import com.hongyi.modelcontest.modular.wechat.service.WxService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/wx")
public class WxController {
    private static String PREFIX = "/test/";
    @Resource
    private WxService wxService;
    @Resource
    private UserService userService;

    @RequestMapping("/index")
    public String index() {
        return PREFIX + "index.html";
    }

    @RequestMapping("")
    public void weChat(@RequestParam(required = false) String state, HttpServletResponse res) {
        try {
            res.sendRedirect(wxService.redirectUrl(state));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/re")
    public void wx(@RequestParam(name = "signature", required = false) String signature,
                   @RequestParam(name = "timestamp", required = false) String timestamp,
                   @RequestParam(name = "nonce", required = false) String nonce,
                   @RequestParam(name = "echostr", required = false) String echostr,
                   HttpServletResponse response) {
        String s = wxService.checkWxServer(timestamp, nonce);
        if (signature.equals(s)) {
            try {
                response.getWriter().write(echostr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/sign")
    @ResponseBody
    public ResponseData sign(@RequestParam("url") String url) {
        return ResponseData.success(wxService.sign(url));
    }

    @RequestMapping("/openId")
    public void getOpenId(@RequestParam("code") String code,
                          @RequestParam(required = false) String state, HttpServletResponse res) {
        res.setHeader("Content-type", "text/html;charset=UTF-8");
        res.setCharacterEncoding("UTF-8");
        try {
            String openId = wxService.getOpenId(code);
            if (StringUtils.isEmpty(openId)) {
                res.getWriter().write("获取openId错误");
                return;
            }
            String url;
            String states = Base64Util.decode(state);
            if (states.contains("R=agentregister")) {
                url = SpringbootUtils.getRemotUrl() + "/agent/index.html?openId=" + openId +
                        Base64Util.decode(state);
            } else {

                url = SpringbootUtils.getRemotUrl() + "/customer/index.html?openId=" + openId +
                        Base64Util.decode(state);
            }
            User agent = userService.getByOpenId(openId);
            if (agent != null) {
                url += "&A=" + agent.getUserId();
            }
            res.sendRedirect(url);

        } catch (Exception e) {
            e.printStackTrace();
            try {
                res.getWriter().write("访问错误");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

 /*   @GetMapping("/openId")
    @ResponseBody
    public ResponseData openId(@RequestParam("code") String code) {
        return ResponseData.success(wxService.getOpenId(code));
    }*/

}
